// pages/login/login.js
let app = getApp()
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        nickName: "",
        password: "ljy8087",
        phone: "18378560681"

    },
    // 注册
    register() {
        let nickName = this.data.nickName
        let password = this.data.password
        let phone = this.data.phone
        if (nickName == '' || password == '' || phone == '') {
            wx.showToast({
                title: '请填写完整信息',
                icon: 'error'
            })
        } else {
            wx.request({
                url: 'http://www.kangliuyong.com:10002/register',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    phone: phone,
                    nickName: nickName,
                    password: password
                },
                success: (res) => {
                    console.log(res);
                    const db = wx.cloud.database()
                    db.collection('user').where({
                            phone: phone
                        }).get()
                        .then(res => {
                            console.log(res);
                            if (res.data.length > 0) {
                                wx.showToast({
                                    title: '手机号已注册',
                                    icon: 'error'
                                })
                            } else {
                                db.collection('user').add({
                                    data: {
                                        nickName: nickName,
                                        password: password,
                                        phone: phone
                                    }
                                }).then(res => {
                                    console.log(res);
                                    if (res.errMsg == 'collection.add:ok') {
                                        wx.showToast({
                                            title: '注册成功',
                                        })

                                    } else {
                                        wx.showToast({
                                            title: '注册失败',
                                            icon: 'error'
                                        })
                                    }
                                })
                            }
                        })
                }
            })
        }

    },
    // 登录
    login() {
        let password = this.data.password
        let phone = this.data.phone
        console.log(phone);
        console.log(password);
        if (password == '' || phone == '') {
            wx.showToast({
                title: '手机号或密码不能为空',
                icon: 'error'
            })
        } else {
            db.collection('user').where({
                    phone: phone
                }).get()
                .then(res => {
                    console.log(res);
                    if (res.data.length == 0) {
                        wx.showToast({
                            title: '手机号不存在',
                            icon: 'error'
                        })
                    }
                    // if(res.data.length>0){
                    //     // 登录成功保存登录信息
                    //     wx.setStorage({
                    //         key: 'userInfo',
                    //         data: res.data[0],
                    //         success: re => {
                    //             app.globalData.userInfo = res.data[0]
                    //         }
                    //     })
                    // }
                    if (password == res.data[0].password) {
                        wx.showToast({
                            title: '登录成功',
                        })
                        console.log(phone);
                        console.log(password);
                        console.log(res.data[0].password);
                        wx.request({
                            url: 'http://www.kangliuyong.com:10002/login',
                            method: "POST",
                            header: {
                                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                            },
                            data: {
                                appkey: app.globalData.appkey,
                                password: password,
                                phone: phone
                            },
                            success: (res) => {
                                // 登录成功获取token
                                console.log(res);
                                if (res.data.code == 200) {
                                    wx.showToast({
                                        title: res.data.msg,
                                    })
                                    // 保存token
                                    wx.setStorage({
                                        key: 'token',
                                        data: res.data.token,
                                        success: re => {
                                            app.globalData.token = res.data.token
                                        }
                                    })
                                    wx.switchTab({
                                        url: '/pages/mine/mine',
                                    })
                                } else {
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: 'error'
                                    })
                                }
                            }
                        })
                    } else {
                        wx.showToast({
                            title: '密码错误',
                            icon: 'error'
                        })
                    }
                })
        }
    },
    // 显示隐藏注册
    showRegister() {
        let show = this.data.show
        this.setData({
            show: !show,
        })
    },
    retrieve() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/retrievePassword',
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
            },
            data: {
                appkey: app.globalData.appkey,
                phone: 18378560681,
                password: 'ljy8087'
            },
            success: res => {
                console.log(res);
            }
        })
    }
})